<?php

require_once( dirname(__FILE__).'/./libs/bedmintonlib.php' );

if (! isset( $_POST[ 'encryptedData' ] ))
{
    echo( "false" );
    exit;
}
    
$encryptedData = $_POST[ 'encryptedData' ];
$key = "Ts8dI9ATlao3a0GY";
$iv = "x4DZbX4UQ0mYlDaH";
$method = "aes-128-cbc";

$encryptedData = hexToStr( $encryptedData );
if (strlen($encryptedData) <= 32)
{
    echo( "false" );
    exit;
}

$decryptedDataFirst = openssl_decrypt( $encryptedData, $method, $key, true, $iv );
$key = substr( $decryptedDataFirst, 0, 16 );
$iv = substr( $decryptedDataFirst, 16, 32 );
$decryptedDataFirst = substr( $decryptedDataFirst, 32 );
$decryptedData = openssl_decrypt( $decryptedDataFirst, $method, $key, true, $iv );
 
$smtpHost;
$smtpPort;  
$from;
$mailFrom;
$mailFromPassword;
$to;
$mailTo;
$subjectStr;
$bodyStr;
$explodedData = explode( "|", $decryptedData );

$c = count( $explodedData );
for ($i = 0; $i < $c; $i ++)
{   
    $p = explode( ";", $explodedData[ $i ] );
    if ($p[ 0 ] == "smtpHost")
    {
        $smtpHost = hexToStr( $p[ 1 ] );
    }
    else if ($p[ 0 ] == "smtpPort")
    {
        $smtpPort = hexToStr( $p[ 1 ] );
    }
    else if ($p[ 0 ] == "from")
    {
        $from = hexToStr( $p[ 1 ] );
    }
    else if ($p[ 0 ] == "mailFrom")
    {
        $mailFrom = hexToStr( $p[ 1 ] );
    }
    else if ($p[ 0 ] == "mailFromPassword")
    {
        $mailFromPassword = hexToStr( $p[ 1 ] );
    }
    else if ($p[ 0 ] == "to")
    {
        $to = hexToStr( $p[ 1 ] );
    }
    else if ($p[ 0 ] == "mailTo")
    {
        $mailTo = hexToStr( $p[ 1 ] );
    }
    else if($p[ 0 ] == "subjectStr")
    {
        $subjectStr = hexToStr( $p[ 1 ] );
    }
    else if($p[ 0 ] == "bodyStr")
    {
        $bodyStr = hexToStr( $p[ 1 ] );
    }
    else
    {
        echo( "false" );
        exit;
    }
}

if( !isset( $smtpHost ) ||
    !isset( $smtpPort ) ||
    !isset( $from ) ||
    !isset( $mailFrom ) ||
    !isset( $mailFromPassword ) ||
    !isset( $to ) ||
    !isset( $mailTo ) ||
    !isset( $subjectStr ) ||
    !isset( $bodyStr ))
{
    echo( "false" );
    exit;
}
    
$mail = new PHPMailer( true ); 
$mail->IsSMTP(); 
try 
{  
    $mail->CharSet    = "UTF-8";
    $mail->Host       = $smtpHost; 
    $mail->SMTPAuth   = true;                  
    $mail->Port       = $smtpPort;                    
    $mail->Username   = $mailFrom; 
    $mail->Password   = $mailFromPassword;        
    $mail->AddAddress( $mailTo, $to );
    $mail->SetFrom( $mailFrom, $from );
    $mail->Subject = $subjectStr;
    $mail->Body = $bodyStr;
    $mail->Send();
    echo( "true" );
} 
catch (phpmailerException $e) 
{
    echo( "false" );
} 
catch (Exception $e) 
{
    echo( "false" );
}

?>
